package com.baizhi.service.serviceImpl;

import com.baizhi.dao.UserDAO;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;


/**
 * @Auther: dlh
 * @Date: 2020/11/12
 * @Descible:${name}
 * @Version:1.0
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    /**
     * 用户登录方法
     * @param user
     */
    @Override
    public User login(User user) {
        User UserDB = userDAO.findByUserName(user.getUsername());
        if (!ObjectUtils.isEmpty(UserDB)) {
            if (UserDB.getPassword().equals(user.getPassword())) {
                return UserDB;
            } else {
              throw new RuntimeException("密码错误！！！");
            }

        } else {
            throw new RuntimeException("用户名输入错误");
        }
    }
    @Override
    public void save(User user) {
        //查询用户账号是否存在
        User UserDB = userDAO.findByUserName(user.getUsername());
        if (UserDB == null) {
            user.setStatus("已激活");
            user.setRegisterTime(new Date());
            userDAO.save(user);
        } else {
            throw new RuntimeException("用户名存在！！！");
        }


    }
}
